package com.example.finalassignment.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.example.finalassignment.common.Constant;
import com.example.finalassignment.exception.ServiceException;
import com.example.finalassignment.mapper.FilesMapper;
import com.example.finalassignment.pojo.Files;
import com.example.finalassignment.pojo.response.ResultResponse;
import com.example.finalassignment.service.FilesService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class FilesServiceImpl implements FilesService {

    //视频后缀
    String videoSuffix = "mp3,mp4,m3u8";

    private final FilesMapper filesMapper;

    public FilesServiceImpl(FilesMapper fileMapper) {
        this.filesMapper = fileMapper;
    }

    @Override
    public ResultResponse upload(MultipartFile file) throws IOException {
        //获取名字
        String originalFilename = file.getOriginalFilename();

        //获取文件的类型
        String type = FileUtil.extName(originalFilename);

        //获取长度
        long size = file.getSize();

        if (type != null && videoSuffix.contains(type)) {
            return saveVideo(file, type, originalFilename, size);
        }
        return saveFile(file, type, originalFilename, size);
    }
    @Override
    public String[] getFileAddress(String uuid) {
        String type = filesMapper.getTypeByUUID(uuid);

        if ("".equals(type) || type == null) {
            throw new ServiceException(201,"文件不存在");
        }
        String[] strings = new String[5];

        if(videoSuffix.contains(type)){
            strings[0] = Constant.VIDEO_ADDRESS + "/" + uuid + StrUtil.DOT;
        }else {
            strings[0] = Constant.FILE_ADDRESS + "/" + uuid + StrUtil.DOT;
        }
        strings[1] = type;

        // 返回uuid 对应的文件地址 和 文件类型
        return strings;
    }

    /**
     * 存放图片或文件
     * @param file
     * @param type
     * @param originalFilename
     * @param size
     * @return
     */
    private ResultResponse saveFile(MultipartFile file, String type, String originalFilename, long size) {
        //判断 当前存储的地址 存不存在 不存在就创建
        File uploadPathFile = new File(Constant.FILE_ADDRESS);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdirs();
        }

        // 生成uuid
        String uuid = IdUtil.simpleUUID();

        try {
            //存储到磁盘上
            File uploadFile = new File(Constant.FILE_ADDRESS + "/" + uuid + StrUtil.DOT + type);
            file.transferTo(uploadFile);
            //数据存储到数据库
            Files fileDocument = new Files();
            fileDocument.setId(0);
            fileDocument.setName(originalFilename);
            fileDocument.setType(type);
            fileDocument.setUuid(uuid);
            fileDocument.setSize(String.valueOf(size));
            fileDocument.setCreateTime(new Date());
            filesMapper.insertSelective(fileDocument);

            return ResultResponse.success(uuid);
        } catch (Exception e) {
            throw new ServiceException(201,e.getMessage());
        }
    }

    /**
     * 存放视频
     * @param file
     * @param type
     * @param originalFilename
     * @param size
     * @return
     */
    private ResultResponse saveVideo(MultipartFile file, String type, String originalFilename, long size) {
        //判断 当前存储的地址 存不存在 不存在就创建
        File uploadPathFile = new File(Constant.VIDEO_ADDRESS);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdirs();
        }

        // 生成uuid
        String uuid = IdUtil.simpleUUID();

        try {
            //存储到磁盘上
            File uploadFile = new File(Constant.VIDEO_ADDRESS + "/" + uuid + StrUtil.DOT + type);
            file.transferTo(uploadFile);
            //数据存储到数据库
            Files fileDocument = new Files();
            fileDocument.setId(0);
            fileDocument.setName(originalFilename);
            fileDocument.setType(type);
            fileDocument.setUuid(uuid);
            fileDocument.setSize(String.valueOf(size));
            fileDocument.setCreateTime(new Date());
            filesMapper.insertSelective(fileDocument);

            return ResultResponse.success(uuid);
        } catch (Exception e) {
            throw new ServiceException(201,e.getMessage());
        }
    }

}
